package com.renge.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.renge.eduservice.entity.EduSubject;
import com.renge.eduservice.entity.easyExcel.SubjectData;
import com.renge.eduservice.entity.easyExcel.SubjectExcelListen;
import com.renge.eduservice.entity.json.SubjectJson;
import com.renge.eduservice.mapper.EduSubjectMapper;
import com.renge.eduservice.service.EduSubjectService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-02
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void addSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            EasyExcel.read(file.getInputStream(), SubjectData.class,new SubjectExcelListen(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    //返回tree的结构
    public List<SubjectJson> getSubjectJsons() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id","0");
        //获得root节点集合
        List<EduSubject> list = this.list(queryWrapper);
        //定义集合存放root节点
        ArrayList<SubjectJson> Jsons = new ArrayList<>();
        //循环root节点，转成SubjectJson格式
        for(EduSubject eduSubject:list ){
            SubjectJson subjectJson = new SubjectJson();
            subjectJson.setId(eduSubject.getId());
            subjectJson.setLabel(eduSubject.getTitle());
            Jsons.add(subjectJson);
        }
        //循环得到root子节点
        for(SubjectJson json :Jsons){
             fromEduSubject(json);
        }
        return Jsons;
    }
    //根据前端传来的一级求二级
    @Override
    public List<EduSubject> findAllByParentId(String parentId) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<EduSubject> list = this.list(wrapper);
        return list;
    }


    //求根子节点并封装其子节点
    private SubjectJson fromEduSubject(SubjectJson json){
        //获得子节点
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",json.getId());
        List<EduSubject> list = this.list(queryWrapper);
        //判断是否有子节点
        if(list.size()>0){
            //用于存放子节点
            ArrayList<SubjectJson> subjectJsons = new ArrayList<>();
            for (EduSubject es:list){
                //将子节点先行封装成subjectJson，加入children
                SubjectJson subjectJson = new SubjectJson();
                subjectJson.setId(es.getId());
                subjectJson.setLabel(es.getTitle());
                subjectJsons.add(subjectJson);
                //子节点继续迭代递归
                fromEduSubject(subjectJson);
            }
            //把子节点放进节点
            json.setChildren(subjectJsons);
        }

        return json;
    }
    //前端封装所有SubjectJson
    @Override
    public List<SubjectJson> findAllSubject() {
        //获得所有一级类别
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> oneList = this.list(wrapper);
        if(StringUtils.isEmpty(oneList)){
            return null;
        }
        List<SubjectJson> jsons=new ArrayList<>();
        //遍历一级类别得到SubjectJson
        for(EduSubject subject:oneList){
            SubjectJson subjectJson = new SubjectJson();
            subjectJson.setId(subject.getId());
            subjectJson.setLabel(subject.getTitle());
            jsons.add(subjectJson);
        }
        //得到二级或多级类别
        for(SubjectJson json:jsons){
            getToJect(json);
        }

        return jsons;
    }
    public SubjectJson getToJect(SubjectJson subjectJson){
        //判断参数是否有二级或者多级
        String JsonPId = subjectJson.getId();
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",JsonPId);
        List<EduSubject> list = this.list(wrapper);
        if (list.size()>0){
            ArrayList<SubjectJson> children = new ArrayList<>();
             for(EduSubject e:list){
                 SubjectJson su = new SubjectJson();
                 su.setId(e.getId());
                 su.setLabel(e.getTitle());
                 children.add(su);
                 getToJect(su);
             }
            subjectJson.setChildren(children);
        }
        return subjectJson;
    }

}
